package com.valentinalee.bms.web.servlet.filemanage;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.valentinalee.bms.entity.FileSystemVO;
import com.valentinalee.bms.entity.ManagerVO;
import com.valentinalee.bms.service.FileSystemService;
import com.valentinalee.bms.service.impl.FileSystemServiceImpl;

public class FileDelServlet extends HttpServlet {
	FileSystemService iFileSystem = new FileSystemServiceImpl();
	/**
	 * Constructor of the object.
	 */
	public FileDelServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		ManagerVO mv = (ManagerVO)request.getSession().getAttribute("currentManager");
		//日期格式转换
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String path ="E:\\二阶段项目\\bms\\WebRoot\\WEB-INF\\upload\\";
		String fileName = request.getParameter("filename");
//		String wzPath = path+fileName;
		File file = new File(path+fileName);
		if(!file.exists()){
			response.getWriter().write("文件不存在");
			//页面跳转
		}else{
			file.delete();
			//添加记录删除记录
			FileSystemVO vo = new FileSystemVO();
			vo.setFileName(fileName);
			vo.setFilePath(path);
			vo.setFileUser(mv.getName());
			vo.setHandleTime(sdf.format(new Date()));
			vo.setHandleFlag("dt");//表示删除
			try {
				iFileSystem.setFileRmark(vo);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.getWriter().write("文件已删除");
		}
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
